﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;


namespace lxwmvc
{
    /// <summary>
    /// 图像处理类
    /// </summary>
    public class imagehelper 
    {
        /// <summary>
        /// 默认切割
        /// </summary>
        public static Bitmap cutsuoxiaimage(Bitmap bmp, int[] size)
        {
            return cutsuoxiaoimage(bmp, size, true, true);
        }

        /// <summary>
        /// 默认切割
        /// </summary>
        public static Bitmap cutsuoxiaimage(Bitmap bmp, Size size)
        {
            return cutsuoxiaoimage(bmp, new int[] { size.Width, size.Height }, true, true);
        }

        /// <summary>
        /// 图片进行处理
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="size"></param>
        /// <param name="isCut"></param>
        /// <returns></returns>
        public static Bitmap cutsuoxiaoimage(Bitmap bmp, int[] size, bool isCut, bool isblank)
        {
            System.Drawing.Image image = bmp.Clone() as System.Drawing.Image;

            if (isCut) image = imagecut(image, size);
            Bitmap m = (Bitmap)getminpic(image, size);

            //if (m.Width != size[0] || m.Height != size[1])
            //    return new Bitmap(m, size[0], size[1]);
            if (isblank)
            {
                Bitmap map = new Bitmap(size[0], size[1], m.PixelFormat);
                float x = (size[0] - m.Width) / 2f;
                float y = (size[1] - m.Height) / 2f;

                Graphics newBmpGraphics = Graphics.FromImage(map);
                newBmpGraphics.Clear(Color.White);
                newBmpGraphics.DrawImage(m, x, y);
                newBmpGraphics.Save();
                newBmpGraphics.Dispose();

                m = map;
            }

            return m;
        }

        /// <summary>
        /// 保存并返回根目录路径
        /// </summary>
        /// <param name="img"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string saveimage(System.Drawing.Image img, string path = "")
        {
            string strUrl = string.Concat(new object[] { "/Upload/img/", DateTime.Now.Year, "/", DateTime.Now.Month, "/", DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff"), ".jpg" });

            //这里外部给出地址
            if (!string.IsNullOrEmpty(path)) strUrl = path;
            string strNewFilePath = HttpContext.Current.Server.MapPath(strUrl);
            Directory.CreateDirectory(Path.GetDirectoryName(strNewFilePath));
            img.Save(strNewFilePath, ImageFormat.Jpeg);

            return strUrl;
        }

        public static Bitmap cutimage(Bitmap bmp, int[] size)
        {
            System.Drawing.Image image = bmp.Clone() as System.Drawing.Image;

            image = imagecut(image, size);
            return (Bitmap)getminpic(image, size);

        }


        /// <summary>
        /// 图片切割
        /// </summary>
        /// <param name="image"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static System.Drawing.Image imagecut(System.Drawing.Image image, int[] size)
        {
            if (!checksize(image, size)) return image;

            double p = 0.00;
            Rectangle rect = new Rectangle(0, 0, 0, 0);

            if (image.Size.Height / (double)size[1] > image.Size.Width / (double)size[0])
            {
                p = image.Size.Width / (double)size[0];
                rect.Y = (int)Math.Floor((image.Size.Height - size[1] * p) / 2);

                rect.Width = image.Width;
                rect.Height = (int)Math.Floor(size[1] * p);
            }
            else
            {
                p = image.Size.Height / (double)size[1];
                rect.X = (int)Math.Floor((image.Size.Width - size[0] * p) / 2);

                rect.Height = image.Height;
                rect.Width = (int)Math.Floor(size[0] * p);
            }

            Bitmap newBmp = new Bitmap(rect.Width, rect.Height, image.PixelFormat);
            Graphics newBmpGraphics = Graphics.FromImage(newBmp);
            newBmpGraphics.DrawImage(image, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
            newBmpGraphics.Save();

            image.Dispose();

            return newBmp;
        }

        /// <summary>
        /// 生成小的图片
        /// </summary>
        /// <param name="MaxPic"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        static private System.Drawing.Image getminpic(System.Drawing.Image MaxPic, int[] size)
        {
            int height = 0;
            int width = 0;
            if ((MaxPic.Height <= size[1]) && (MaxPic.Width <= size[0]))
            {
                height = MaxPic.Height;
                width = MaxPic.Width;
                goto End;
            }
            if (size[1] * MaxPic.Width == MaxPic.Height * size[0])
            {
                width = size[0];
                height = size[1];
                goto End;
            }
            else if (MaxPic.Height * size[0] < MaxPic.Width * size[1])
            {
                width = size[0];
                height = (int)Math.Floor(MaxPic.Height / (MaxPic.Width / (double)size[0]));
                goto End;
            }
            else
            {
                width = (int)Math.Floor(MaxPic.Width / (MaxPic.Height / (double)size[1]));
                height = size[1];
                goto End;
            }

        End:
            {
                Bitmap newBmp = new Bitmap(width, height, MaxPic.PixelFormat);
                Graphics newBmpGraphics = Graphics.FromImage(newBmp);
                newBmpGraphics.DrawImage(MaxPic, new Rectangle(0, 0, width, height), new Rectangle(0, 0, MaxPic.Width, MaxPic.Height), GraphicsUnit.Pixel);
                newBmpGraphics.Save();
                newBmpGraphics.Dispose();
                return newBmp;
            }
        }

        static private bool checksize(System.Drawing.Image img, int[] size)
        {
            return (img.Height > size[1] || img.Width > size[0]);
        }

        static private bool checknowarter(Size _Size, int[] size)
        {
            return (_Size.Height < size[1] || _Size.Width < size[0]);
        }


        /// <summary>
        /// 得到图片缩略图
        /// </summary>
        /// <param name="img">图片对象</param>
        /// <param name="maxWidth">最大宽度</param>
        /// <param name="maxHeight">最大高度</param>
        /// <param name="isBlock">等比缩放</param>
        /// <param name="size">用时</param>
        /// <returns></returns>
        public static System.Drawing.Image GetThumbImage(System.Drawing.Image img, int maxWidth, int maxHeight, bool isBlock, out float size)
        {
            int tWidth;
            int tHeight;
            size = 1;

            if (!isBlock)
            {
                tWidth = maxWidth;
                tHeight = maxHeight;
            }
            else
            {
                if (!(maxHeight > img.Height && maxWidth > img.Width))
                {
                    float HeightMultipier = (float)maxHeight / (float)img.Height;
                    float WidthMultipier = (float)maxWidth / (float)img.Width;
                    if (HeightMultipier > 1) HeightMultipier = 1;
                    if (WidthMultipier > 1) WidthMultipier = 1;
                    float SizeMultiplier = WidthMultipier < HeightMultipier ? WidthMultipier : HeightMultipier;
                    size = SizeMultiplier;
                    tWidth = (int)(img.Width * SizeMultiplier);
                    tHeight = (int)(img.Height * SizeMultiplier);
                }
                else
                {
                    tWidth = img.Width;
                    tHeight = img.Height;
                }
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(tWidth, tHeight);

            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(img, new Rectangle(0, 0, tWidth, tHeight),
                new Rectangle(0, 0, img.Width, img.Height),
                GraphicsUnit.Pixel);
            return bitmap;
        }
    }
}